// src/modules/project/api.ts
import { api } from "@/lib/api/requests";
import { PROJECT_API } from "./endpoints";
import type {
  Project,
  ProjectCreate,
  ProjectUpdate,
  ProjectQuery,
  ProjectListResponse,
} from "./types";

/* ---------------- API ---------------- */
export const projectApi = {
  // 获取项目列表
  async listProjects(q?: ProjectQuery): Promise<ProjectListResponse> {
    const query: Record<string, string> = {};
    if (q?.page) query.page = String(q.page);
    if (q?.page_size) query.page_size = String(q.page_size);
    if (q?.project_number) query.project_number = q.project_number;
    if (q?.project_type) query.project_type = q.project_type;
    if (q?.status) query.status = q.status;

    const resp = await api.get<ProjectListResponse>(PROJECT_API.projects.list, { query });
    return resp;
  },

  // 创建项目
  async createProject(payload: ProjectCreate): Promise<Project> {
    const resp = await api.post<Project, ProjectCreate>(
      PROJECT_API.projects.create,
      payload
    );
    return resp;
  },

  // 获取特定项目详情
  async getProject(id: string): Promise<Project> {
    const resp = await api.get<Project>(PROJECT_API.projects.detail(id));
    return resp;
  },

  // 更新项目
  async updateProject(payload: ProjectUpdate): Promise<boolean> {
    const resp = await api.post<boolean, ProjectUpdate>(
      PROJECT_API.projects.update,
      payload
    );
    return resp;
  },

  // 删除项目
  async deleteProject(id: string): Promise<boolean> {
    const resp = await api.post<boolean, { id: string }>(
      PROJECT_API.projects.delete,
      { id }
    );
    return resp;
  },
};
